import os
import json
import sys
import template
import analy_redis
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from tornado.options import define, options

define("port", default=8080, help="run on the given port", type=int)
define("redisport", default=6379, help="redis run on the given port", type=int)
define("redishost", default='localhost', help="run on this server")

redis_client = None 

class OptionHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(redis_client.all_options())
class SetHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(redis_client.all_options())
    def post(self):
        self.set_header("Content-Type", "text/plain")
        log_dir = self.get_argument("log_dir")
        time = self.get_argument("time")
        opts = {'log_dir':log_dir, 'time':time}
        template.main(opts)
        os.system("service rsyslog restart")
        self.write("set ok")
class TotalqueryHandler(tornado.web.RequestHandler):
    def get(self, server, view):
        print "server: %s view: %s totalquery" % (server, view )
        self.write(redis_client.get_totalquery(server + ':' + view))
class ViewqpsHandler(tornado.web.RequestHandler):
    def set_default_headers(self): 
        self.set_header('Access-Control-Allow-Origin', '*')
    def get(self, server, view):
        self.write(redis_client.get_view_qps(server + ':' + view))
class DomainqpsHandler(tornado.web.RequestHandler):
    def get(self, server, view):
        domain = self.get_argument("para", None)
        print view, domain
        self.write(redis_client.get_domain_qps(server + ':' + view, domain))
class ViewHandler(tornado.web.RequestHandler):
    def get(self, server, view, key):
        print view, key
        parameter = self.get_argument("topn", None)
        if not parameter:
            parameter = self.get_argument('para', None)
        if parameter:
            self.write(redis_client.execute_query(server + ':' + view, key, parameter))
        else:
            self.write("parameter error")
class PropertyHandler(tornado.web.RequestHandler):
    def get(self, server, view, key):
        print view, key
        self.write(redis_client.get_property(server + ':' + view, key))
class IPHandler(tornado.web.RequestHandler):
    def get(self, server, view):
        print view
        self.write(redis_client.get_ipaddr(server + ':' + view))

class DomainHandler(tornado.web.RequestHandler):
    def get(self, server, view):
        parameter = self.get_argument("para", None)
        num = self.get_argument("topn", None)
        print "----", parameter,num 
        if parameter and num:
            self.write(redis_client.get_mutual_info(server + ':' + view, parameter, num))
        else: 
            self.write("parameter error")
class ViewallHandler(tornado.web.RequestHandler):
    def get(self, server, view):
        result = redis_client.get_all_stats(server + ':' + view)
        self.write(result)

def main():
    tornado.options.parse_command_line()
    global redis_client
    redis_client = analy_redis.Log_analy(options.redishost,options.redisport)
    application = tornado.web.Application([
        ('/options', OptionHandler),
        ('/set', SetHandler),
        ('/servers/(.*)/views/(.*)/stats/totalquery', TotalqueryHandler),
        ('/servers/(.*)/views/(.*)/stats/qps', ViewqpsHandler),
        ('/servers/(.*)/views/([^\/]+)', ViewallHandler),
        ('/servers/(.*)/views/(.*)/stats/domainqps', DomainqpsHandler),
        ('/servers/(.*)/views/(.*)/stats/ipdomain', DomainHandler),
        ('/servers/(.*)/views/(.*)/stats/domainip', DomainHandler),
        ('/servers/(.*)/views/(.*)/stats/(domaintopn)', ViewHandler),
        ('/servers/(.*)/views/(.*)/stats/(iptopn)', ViewHandler),
        ('/servers/(.*)/views/(.*)/stats/ipaddr', IPHandler),
        ('/servers/(.*)/views/(.*)/stats/(rtype|recursion|rcode|tcp|edns|dnssec|cd|sign)', PropertyHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    print "server start at port: ", options.port
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
